package file;

import game.Russian2.S;

import java.io.*;

public class FileDemo {
    public static void main(String[] args) throws IOException {

        String str="我爱你";
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.println(chars[i]);
        }
        char  a='我';
        byte[] bytes = str.getBytes();


        method7();
//        mothed7();
//        mothed6();
//        mothed5();
//        mothed11();
//
//
//        long start = System.currentTimeMillis();
//        mothed1();//字节流的基本流：一次读写一个字节
//        long end = System.currentTimeMillis();
//        System.out.println("字节流的基本流：一次读写一个字节" + (end - start) / 1000.0 + "秒");
//
//        start = System.currentTimeMillis();
//        mothed2();//字节流的基本流：一次读写一个字节数组
//        end = System.currentTimeMillis();
//        System.out.println("字节流的基本流：一次读写一个字节数组" + (end - start) / 1000.0 + "秒");
//
//        start = System.currentTimeMillis();
//        mothed3();//字节缓冲流：一次读写一个字节
//        end = System.currentTimeMillis();
//        System.out.println("字节缓冲流：一次读写一个字节" + (end - start) / 1000.0 + "秒");
//
//        start = System.currentTimeMillis();
//        mothed4();//字节缓冲流：一次读写一个字节数组
//        end = System.currentTimeMillis();
//        System.out.println("字节缓冲流：一次读写一个字节数组" + (end - start) / 1000.0 + "秒");

    }

    /**
     * 将字符串转化成字节数组
     *
     * @throws IOException
     */
    private static void mothed11() throws IOException {
        String str = "我爱你";
        byte[] b = str.getBytes();
        System.out.println(b);


    }

    private static void mothed4() throws IOException {
        String pathstart = "D:\\javadevelop\\javastart\\src\\javaNotebook\\3.txt";
        String pathend = "src\\copy4.txt";
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pathstart));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(pathend));
        int len;
        byte[] b = new byte[10];
        while ((len = bis.read(b)) != -1) {
            bos.write(b, 0, len);
        }
        bos.close();
        bis.close();


    }

    private static void mothed3() throws IOException {
        String pathstart = "D:\\javadevelop\\javastart\\src\\javaNotebook\\3.txt";
        String pathend = "src\\copy3.txt";
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pathstart));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(pathend));
        int b;
        while ((b = bis.read()) != -1) {
            bos.write(b);
        }
        bos.close();
        bis.close();
    }

    private static void mothed2() throws IOException {
        String pathstart = "D:\\javadevelop\\javastart\\src\\javaNotebook\\3.txt";
        String pathend = "src\\copy2.txt";
        FileInputStream fis = new FileInputStream(pathstart);
        FileOutputStream fos = new FileOutputStream(pathend);
        int len;
        byte[] b = new byte[10];
        while ((len = fis.read(b)) != -1) {
            fos.write(b, 0, len);
        }
        fos.close();
        fis.close();
    }

    private static void mothed1() throws IOException {
        String pathstart = "C:\\Users\\hongtang.yang\\Documents\\企知道\\生产账号.txt";
        String pathend = "C:\\Users\\hongtang.yang\\Documents\\企知道\\生产账号1.txt";
        FileInputStream fis = new FileInputStream(pathstart);
        FileOutputStream fos = new FileOutputStream(pathend);
        int b;
        while ((b = fis.read()) != -1) {
            fos.write(b);
        }
        fos.close();
        fis.close();
    }

    /**
     * 字符流的输入输出
     */
    private static void mothed5() throws IOException {
        String pathstart = "C:\\Users\\hongtang.yang\\Documents\\企知道\\生产账号.txt";
        String pathend = "C:\\Users\\hongtang.yang\\Documents\\企知道\\生产账号10.txt";
        FileReader fr = new FileReader(pathstart);
        FileWriter fw = new FileWriter(pathend);
        char[] b = new char[10];
        while (fr.read(b) != -1) {
            fw.write(b);
        }
        fw.close();
        fr.close();

    }

    /**
     * 字符缓冲流
     */
    private static void mothed6() throws IOException {
        String pathstart = "C:\\Users\\hongtang.yang\\Desktop\\java 集合学习.doc";
        String pathend = "C:\\Users\\hongtang.yang\\Desktop\\java 集合学习1.doc";
        BufferedReader br = new BufferedReader(new FileReader(pathstart));
        BufferedWriter bw = new BufferedWriter(new FileWriter(pathend));
        char[] b = new char[10];
        while (br.read(b) != -1) {
            bw.write(b);
        }
        bw.close();
        br.close();
    }
    /**
     *  转换流
     *  1.字节流和字符流之间的转换：InputStreamReader
     *  2.字节流和字符流之间的转换:OutputStreamWriter
     */
    private static void mothed7() throws IOException {
        String pathstart = "C:\\Users\\hongtang.yang\\Desktop\\java 集合学习.doc";
        String pathend = "C:\\Users\\hongtang.yang\\Desktop\\java 集合学习1.doc";
        InputStreamReader isr = new InputStreamReader(new FileInputStream(pathstart));
       // OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(pathend));

        while (isr.read() != -1) {

        }
     //   osw.close();
        isr.close();
    }

    /**
     *   字节输入流：FileInputStream
     *   字节输出流：FileOutputStream
     *
     *   字节缓冲输入流：BufferedInputStream
     *   字节缓冲输出流：BufferedOutputStream
     *
     *
     *
     *   字符输入流：FileReader
     *   字符输出流：FileWriter
     *   字符缓冲输入流：BufferedReader
     *   字符缓冲输出流：BufferedWriter
     *
     *
     */

    /**
     *  给一个大文件进行复制，测试字符流和字符缓冲流的性能
     */
    public static void method7() throws IOException {
        File file1 = new File("C:\\Users\\hongtang.yang\\Documents\\企知道\\哈哈哈哈.txt");
        File file2 = new File("C:\\Users\\hongtang.yang\\Documents\\企知道\\企知道\\哈哈哈哈.txt");
        long start = System.currentTimeMillis();
        FileReader fr = new FileReader(file1);
        FileWriter writer = new FileWriter(file2);
        char[] b = new char[10];
        while (fr.read(b) != -1) {
            writer.write(b);
        }
        writer.close();
        fr.close();
        long end = System.currentTimeMillis();
        System.out.println("字符流复制时间：" + (end - start));
        // 字符缓冲流复制时间：
        start = System.currentTimeMillis();
        BufferedReader br = new BufferedReader(new FileReader(file1));
        BufferedWriter bw = new BufferedWriter(new FileWriter(file2));
        char[] b1 = new char[10];
        while (br.read(b1) != -1) {
            bw.write(b1);
        }
        bw.close();
        br.close();
        end = System.currentTimeMillis();
        System.out.println("字符缓冲流复制时间：" + (end - start));
    }

    /**
     *  给一个大文件进行复制，测试字节流和字节缓冲流的性能
     */

    public static void method8() throws IOException {
        File file1 = new File("C:\\Users\\hongtang.yang\\Documents\\企知道\\企知道\\企知道0110产品介绍.pdf");
        File file2 = new File("C:\\Users\\hongtang.yang\\Documents\\企知道\\企知道\\企知道0110产品介绍1.pdf");
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream(file1);
        FileOutputStream fos = new FileOutputStream(file2);
        byte[] b = new byte[200];
        while (fis.read(b) != -1) {
            fos.write(b);
        }
        fos.close();
        fis.close();
        long end = System.currentTimeMillis();
        System.out.println("字节流复制时间：" + (end - start));

        // 字节缓冲流复制时间：
        start = System.currentTimeMillis();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file1));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
        while (bis.read() != -1) {
            bos.write(bis.read());
        }
        bos.close();
        bis.close();
        end = System.currentTimeMillis();
        System.out.println("字节缓冲流复制时间：" + (end - start));


    }
}